package day4;

import day4.Person;

import java.lang.reflect.Field;
import java.util.Date;

//通过反射测试 设置和获取属性
public class TestField {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        //1.获取类对象
        Class c= Person.class;
        //2.获取单个属性
        Field idField=c.getDeclaredField("id");
        Field nameField=c.getDeclaredField("name");
        Field dateField=c.getDeclaredField("birthday");
        idField.setAccessible(true);//开启私有权限
        //3.设置和获取属性值
        Person p=new Person(10);
        //set(对象，属性值) 用于设置属性值
        nameField.set(p,"人机");
        dateField.set(p,new Date());
        idField.set(p,1000);
        //获取属性值
        System.out.println(p.name+" "+p.birthday+"  ");
        //通过反射获取属性值
        //get(对象，属性值) 用于获取属性值
        Object b1=nameField.get(p);
        Object b2=dateField.get(p);
        Object b3=idField.get(p);
        System.out.println(b1+" "+b2+" "+b3);
    }
}
